// 状态消息显示
function showStatus(message, type) {
    const statusDiv = document.getElementById('statusMessage');
    statusDiv.textContent = message;
    statusDiv.className = `status-message ${type}`;
    statusDiv.style.display = 'block';
    setTimeout(() => {
        statusDiv.style.display = 'none';
    }, 3000);
}

// 文件编辑功能
function editFile(scriptName, fileName) {
    fetch(`/edit_file/${scriptName}/${fileName}`)
        .then(response => response.json())
        .then(data => {
            if (data.error) {
                showStatus(data.error, 'error');
                return;
            }

            const editor = document.getElementById('fileEditor');
            editor.value = data.content;
            
            // 更新编辑器标题
            document.getElementById('currentFileName').textContent = fileName.split('/').pop();
            
            // 显示编辑器容器
            document.getElementById('editorContainer').style.display = 'block';
            
            // 更新保存按钮的点击事件
            document.getElementById('saveButton').onclick = () => saveFile(scriptName, fileName);
            
            // 如果是Python文件，显示"设为主文件"按钮
            const setMainButton = document.getElementById('setMainButton');
            if (fileName.toLowerCase().endsWith('.py')) {
                setMainButton.style.display = 'block';
                setMainButton.onclick = () => setMainFile(scriptName, fileName);
            } else {
                setMainButton.style.display = 'none';
            }
        })
        .catch(error => {
            showStatus(`加载文件失败: ${error}`, 'error');
        });
}

// 保存文件内容
function saveFile(scriptName, fileName) {
    const content = document.getElementById('fileEditor').value;
    
    fetch(`/save_file/${scriptName}/${fileName}`, {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json'
        },
        body: JSON.stringify({ content })
    })
    .then(response => response.json())
    .then(data => {
        if (data.error) {
            showStatus(data.error, 'error');
        } else {
            showStatus('文件保存成功', 'success');
        }
    })
    .catch(error => {
        showStatus(`保存失败: ${error}`, 'error');
    });
}

// 设置主文件
function setMainFile(scriptName, fileName) {
    if (!confirm(`确定要将 ${fileName} 设置为主文件吗？`)) {
        return;
    }
    
    window.location.href = `/set_main_file/${scriptName}/${fileName}`;
}

// 目录展开/折叠功能
function toggleDirectory(header) {
    const directory = header.closest('.tree-directory');
    directory.classList.toggle('expanded');
}

// 初始化页面
document.addEventListener('DOMContentLoaded', () => {
    // 为所有目录添加点击事件
    document.querySelectorAll('.directory-header').forEach(header => {
        header.addEventListener('click', () => toggleDirectory(header));
    });

    // 为所有文件添加点击事件
    document.querySelectorAll('.tree-file').forEach(file => {
        file.addEventListener('click', () => {
            const scriptName = file.dataset.script;
            const fileName = file.dataset.file;
            
            // 更新选中状态
            document.querySelectorAll('.tree-file').forEach(f => f.classList.remove('active'));
            file.classList.add('active');
            
            // 加载文件内容
            editFile(scriptName, fileName);
        });
    });

    // 默认展开所有目录
    document.querySelectorAll('.tree-directory').forEach(dir => {
        dir.classList.add('expanded');
    });
}); 